package top.truehaku.generator.code.application.command;

import lombok.Getter;

import java.util.*;

@Getter
public class TargetCode {

    /**
     * 模板标识,字母数字下划线,一组中唯一,必填
     */
    private final String identity;

    /**
     * 模板所在路径(相对路径) 必填
     */
    private final String templatePath;

    /**
     * 子工程路径,子工程相对于项目根路径的位置 选填
     */
    private String subProject;

    /**
     * 功能模块包名
     */
    private String module;

    /**
     * 生成后的文件相对路径(相对于 项目根路径-子工程路径-项目包-模块名称)
     */
    private String relativePath;

    /**
     * 文件名前缀
     */
    private String prefix;

    /**
     * 文件名后缀
     */
    private String suffix;

    /**
     * 扩展名
     */
    private String extension;

    /**
     * 是否可覆盖
     */
    private boolean overridable;

    /**
     * 生成的类是否需要展示数据库字段
     */
    private boolean whetherPojo;

    /**
     * 继承的代码
     */
    private String parentId;

    /**
     * 依赖的的代码
     */
    private List<String> dependIds;

    /**
     * 依赖的包
     */
    private Set<String> imports;

    private TargetCode(String identity, String templatePath){
        this.identity = identity;
        this.templatePath = templatePath;
        this.dependIds = new ArrayList<>();
        this.imports = new HashSet<>();
    }

    public static TargetCode init(String identity, String templatePath){
        return new TargetCode(identity,templatePath);
    }

    public static TargetCode initPojo(String identity, String templatePath){
        TargetCode code = new TargetCode(identity, templatePath);
        code.whetherPojo = true;
        return code;
    }

    public TargetCode setSubproject(String subProject){
        this.subProject = subProject;
        return this;
    }

    public TargetCode setModule(String module){
        this.module = module;
        return this;
    }

    public TargetCode setFileProperty(String relativePath, String prefix, String suffix, String extension){
        this.relativePath = relativePath;
        this.prefix = prefix;
        this.suffix = suffix;
        this.extension = extension;
        return this;
    }

    public TargetCode setOverridable(){
        this.overridable = true;
        return this;
    }

    public TargetCode setParent(TargetCode parent){
        this.parentId = parent.getIdentity();
        return this;
    }

    public TargetCode setDepends(TargetCode...args){
        Arrays.stream(args).map(TargetCode::getIdentity).forEach(dependIds::add);
        return this;
    }

    public TargetCode imports(String ...args){
        this.imports.addAll(Arrays.asList(args));
        return this;
    }

    public TargetCode imports(Class<?> ...args){
        Arrays.asList(args).forEach(clazz->imports.add(clazz.getTypeName()));
        return this;
    }

    public static void main(String[] args) {
        System.out.println(TargetCode.class.getPackage().getName());
    }

}
